package cn.good.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * RedisAPI
 */
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 往redis中缓存数据
     * @param key
     * @param object
     * @return
     */
    public boolean set(String key,Object object){
        ValueOperations<String,Object> vo=redisTemplate.opsForValue();
        vo.set(key,object);
        return true;
    }

    /**
     * 根据key从Redis服务器中获取value值
     * @param key
     * @return
     */
    public Object get(String key){
        ValueOperations<String,Object> vo=redisTemplate.opsForValue();
        return vo.get(key);
    }

    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean exist(String key){
        ValueOperations<String,Object> vo=redisTemplate.opsForValue();
        return EmptyUtils.isEmpty(vo.get(key));
    }

    /**
     * 向redis中缓存数据
     * @param key
     * @param seconds 有效期
     * @param value
     * @return
     */
    public boolean set(String key,int seconds,String value){
        ValueOperations<String,Object> vo=redisTemplate.opsForValue();
        vo.set(key,value,seconds);
        return true;
    }

    /**
     * 删除redis数据
     * @param key
     */
    public void delete(String key){
        redisTemplate.delete(key);
    }
}
